<script setup lang="ts">
import { Confirm, message } from '@/components/XtxUI';
import { useCartStore, useMemberStore } from '@/store';
import { storeToRefs } from 'pinia';
import { useRouter } from 'vue-router';

const cart = useCartStore();
const {
  effectiveList,
  effectiveListCount,
  selectedListCount,
  selectedListPrice,
} = storeToRefs(cart);

// 更新商品选中状态
const updataSelected = (skuId: string, val: boolean) => {
  // console.log('现在可以发请求给后端了', skuId, val);
  cart.updateCart(skuId, { selected: val });
};

// useRouter 注意：
//   useRouter() 需要在 setup 顶级书写
// useRouter 常见错误：
//   1. 非组件中调用 useRouter() 无法获取路由信息
//   2. 事件处理函数中调用 useRouter() 无法获取路由信息

const router = useRouter();
// 下单结算按钮
const goToCheckout = () => {
  // 1. 判断选中的商品数量必须大于 0
  if (selectedListCount.value === 0) {
    return message({ type: 'warn', text: '请选择下单商品~' });
  }
  // 2. 判断用户是否已登录
  const member = useMemberStore();
  if (member.isLogin === false) {
    return message({ type: 'warn', text: '请先登录~' });
  }
  // 跳转页面
  router.push('/member/checkout');
};
// 删除商品
const delCart = async (skuId: string[]) => {
  Confirm({
    title: '温馨提示',
    text: '你确定要删除吗?'
  })
  await cart.deleteCart(skuId)
  message({ type: 'success', text: '删除成功!' })
}
</script>

<template>
  <div class="xtx-cart-page">
    <div class="container">
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem>购物车</XtxBreadItem>
      </XtxBread>
      <div class="cart">
        <table>
          <thead>
            <tr>
              <th width="120">
                <XtxCheckBox :model-value="cart.isAllSelected"
                  @change="cart.updateCartAllSelected(!cart.isAllSelected)">
                  全选
                </XtxCheckBox>
              </th>
              <th width="400">商品信息</th>
              <th width="220">单价</th>
              <th width="180">数量</th>
              <th width="180">小计</th>
              <th width="140">操作</th>
            </tr>
          </thead>
          <!-- 有效商品 -->
          <tbody>
            <tr v-for="item in effectiveList" :key="item.skuId">
              <td>
                <XtxCheckBox :model-value="item.selected" @update:model-value="updataSelected(item.skuId, $event)" />
              </td>
              <td>
                <div class="goods">
                  <RouterLink :to="`/goods/${item.id}`">
                    <img :src="item.picture" alt="" />
                  </RouterLink>
                  <div>
                    <p class="name ellipsis">
                      {{ item.name }}
                    </p>
                    <p class="attr">{{ item.attrsText }}</p>
                  </div>
                </div>
              </td>
              <td class="tc">
                <p>&yen;{{ item.nowPrice }}</p>
              </td>
              <td class="tc">
                <!-- 最大值不能超过商品库存 -->
                <XtxCount :model-value="item.count" @change="cart.updateCart(item.skuId, { count: $event })"
                  :max="item.stock" />
              </td>
              <td class="tc">
                <p class="f16 red">
                  &yen;{{ (Number(item.nowPrice) * item.count).toFixed(2) }}
                </p>
              </td>
              <td class="tc">
                <p><a href="javascript:;">移入收藏夹</a></p>
                <p>
                  <a @click="delCart([item.skuId])" class="green" href="javascript:;">
                    删除
                  </a>
                </p>
                <p><a href="javascript:;">找相似</a></p>
              </td>
            </tr>
            <!-- 删除光购物车之后使用元素占位 -->
            <tr v-if="effectiveList.length === 0">
              <td colspan="6">
                <div class="cart-none" style="text-align: center">
                  <img src="@/assets/images/none.png" alt="" />
                  <p>购物车内暂时没有商品</p>
                  <div class="btn" style="margin: 20px">
                    <XtxButton type="primary"> 继续逛逛 </XtxButton>
                  </div>
                </div>
              </td>
            </tr>
          </tbody>
        </table>
      </div>
      <!-- 操作栏 -->
      <div class="action">
        <div class="batch"></div>
        <div class="total">
          共 {{ effectiveListCount }} 件有效商品，已选择
          {{ selectedListCount }} 件，商品合计：
          <span class="red">¥{{ selectedListPrice }}</span>
          <XtxButton @click="goToCheckout()" type="primary">
            下单结算
          </XtxButton>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped lang="less">
.tc {
  text-align: center;

  .xtx-numbox {
    margin: 0 auto;
    width: 120px;
  }
}

.red {
  color: @priceColor;
}

.green {
  color: @xtxColor;
}

.f16 {
  font-size: 16px;
}

.goods {
  display: flex;
  align-items: center;

  img {
    width: 100px;
    height: 100px;
  }

  >div {
    width: 280px;
    font-size: 16px;
    padding-left: 10px;

    .attr {
      font-size: 14px;
      color: #999;
    }
  }
}

.action {
  display: flex;
  background: #fff;
  margin-top: 20px;
  height: 80px;
  align-items: center;
  font-size: 16px;
  justify-content: space-between;
  padding: 0 30px;

  .xtx-checkbox {
    color: #999;
  }

  .batch {
    a {
      margin-left: 20px;
    }
  }

  .red {
    font-size: 18px;
    margin-right: 20px;
    font-weight: bold;
  }
}

.tit {
  color: #666;
  font-size: 16px;
  font-weight: normal;
  line-height: 50px;
}

.xtx-cart-page {
  .cart {
    background: #fff;
    color: #666;

    table {
      border-spacing: 0;
      border-collapse: collapse;
      line-height: 24px;

      th,
      td {
        padding: 10px;
        border-bottom: 1px solid #f5f5f5;

        &:first-child {
          text-align: left;
          padding-left: 30px;
          color: #999;
        }
      }

      th {
        font-size: 16px;
        font-weight: normal;
        line-height: 50px;
      }
    }
  }
}
</style>
